﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Sinacor.Infra.Service.Data;
using System.Data.Common;

namespace Sinacor.OMM.Configuration.HealthMonitor
{
    /// <summary>
    /// Read health monitor configuration
    /// </summary>
    public class HealthMonitorConfigurationReader : BaseResourceAccess
    {

        private Int32 _companyId;

        #region Constructor

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="companyId"></param>
        public HealthMonitorConfigurationReader(Int32 companyId)
            : base(companyId)
		{
            _companyId = companyId;
		}

		#endregion

        #region Methods

        /// <summary>
        /// Read the receptor configuration
        /// </summary>
        /// <returns></returns>
        public OMM.Configuration.HealthMonitor.HealthMonitorConfiguration GetConfiguration()
        {
            //
            OMM.Configuration.HealthMonitor.HealthMonitorConfiguration healthMonitorConfiguration = null;
            OMM.Configuration.Repository.RepositoryConfigurationReader repositoryReader = null;
            String sqlCommand = String.Empty;

            //
            sqlCommand = @" SELECT  COD_REPRIO_MSG,
                                    TEM_INTV_ATLZ_MONI,
                                    TEM_INTV_NOVO_TENT_CONX,
                                    NOME_DRTO_LOG_FOTO,
                                    NOME_ARQ_LOG_FOTO,
                                    NUM_PRTA_TCP,
                                    NOME_SERD_TCP,
                                    NOME_DRTO_LOG_PROS_SERD
                            FROM TBOCNFG_MONC_SIST ";

            using (DbCommand dbCommand = this.DataBase.GetSqlStringCommand(sqlCommand))
            {
                healthMonitorConfiguration = this.FindOne<OMM.Configuration.HealthMonitor.HealthMonitorConfiguration>(dbCommand,
                    reader =>
                    {
                        OMM.Configuration.HealthMonitor.HealthMonitorConfiguration item = new HealthMonitorConfiguration();

                        item.TimeToRefreshMonitorSeconds = reader.GetValue<Int32>("TEM_INTV_ATLZ_MONI");
                        item.MSMQHealthMonitorQueue.Id = reader.GetValue<Int32>("COD_REPRIO_MSG");
                        item.TimeToRetryConnectionSeconds = reader.GetValue<Int32>("TEM_INTV_NOVO_TENT_CONX");
                        item.SnapshotLogPath = reader.GetValue<String>("NOME_DRTO_LOG_FOTO");
                        item.SnapshotLogFile = reader.GetValue<String>("NOME_ARQ_LOG_FOTO");
                        item.TcpServerPort = reader.GetValue<Int32>("NUM_PRTA_TCP");
                        item.TcpServerHostName = reader.GetValue<String>("NOME_SERD_TCP");
                        item.HealthStatusMonitorServerLogPath = reader.GetValue<String>("NOME_DRTO_LOG_PROS_SERD");

                        return item;

                    });
            }

            if (healthMonitorConfiguration.MSMQHealthMonitorQueue.Id.HasValue)
            {
                repositoryReader = new Sinacor.OMM.Configuration.Repository.RepositoryConfigurationReader(_companyId);
                healthMonitorConfiguration.MSMQHealthMonitorQueue = repositoryReader.GetConfiguration(healthMonitorConfiguration.MSMQHealthMonitorQueue.Id.Value);
            }
            else
            {
                healthMonitorConfiguration.MSMQHealthMonitorQueue = null;
            }

            return healthMonitorConfiguration;
        }

        #endregion

    }
}
